package com.example.nfcaccesssystem;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class UserActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    setContentView(R.layout.activity_user);
	    
	    ListView lw = (ListView)findViewById(R.id.userListView);
		
		lw.setOnItemClickListener(new OnItemClickListener() {
	    	
	    	@Override
	    	public void onItemClick(AdapterView<?> adapter, View view, int position, long arg){
	    		User u = (User) adapter.getItemAtPosition(position);
	    		
	    		Intent i = new Intent(UserActivity.this, EditUserActivity.class);
	    		i.putExtra("user", u);
	    		startActivity(i);
	    	}
		});
	    
	}
	
	@Override
	public void onResume() {
	    super.onResume();
	    
	    Controller.getInstance().getUsers(this);
	}
	
	public void newUser(View v) {
		Intent i = new Intent(this, NewUserActivity.class);
		startActivity(i);
	}
	
	public void setUsers(User[] users){

		ListView lw = (ListView)findViewById(R.id.userListView);
	        
	        if (lw.getAdapter() == null) {
		        final ArrayAdapter<User> adapter = new ArrayAdapter<User>(this, android.R.layout.simple_list_item_1, Controller.getInstance().userList);
		        
				lw.setAdapter(adapter);
	        }
	    
			ArrayAdapter<User> adap = (ArrayAdapter<User>)lw.getAdapter();
	        
	        for (User u : users) {
	        	adap.add(u);
	        }
	        
	        adap.notifyDataSetChanged();
	}
}
